﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace iss.sg4210.se18.team6s.jass.DAL.DataAccess.Utility
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class EmailAttribute : RegularExpressionAttribute
    {
        public EmailAttribute() : base(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$") { }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class IntegerArrayAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            string str = ((string[])value)[0].ToString();
            if (string.IsNullOrEmpty(str))
                return true;

            int getal;
            if (int.TryParse(str, out getal))
                return true;
            return false;
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class IntegerAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            string str = value != null ? value.ToString() : null;
            if (string.IsNullOrEmpty(str))
                return true;

            int getal;
            if (int.TryParse(str, out getal))
                return true;
            return false;
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class DateStartAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            DateTime dateStart = (DateTime)value;
            // Cannot shedule meeting back in the past.
            return (dateStart >= DateTime.Today);
        }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class DateEndAttribute : ValidationAttribute
    {
        public string DateStart { get; private set; }
        public string DateEnd { get; private set; }

        public DateEndAttribute(string dateStart, string dateEnd)
        {
            DateStart = dateStart;
            DateEnd = dateEnd;
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object dateStart = properties.Find(DateStart, true /* ignoreCase */).GetValue(value);
            object dateEnd = properties.Find(DateEnd, true /* ignoreCase */).GetValue(value);


            // Get Value of the DateStart property
            DateTime lDateStart = (DateTime)dateStart;
            DateTime lDateEnd = (DateTime)dateEnd;

            // start time must be before the end time
            return DateTime.Compare(lDateEnd, lDateStart) >= 0;
        }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class IntegerCompareAttribute : ValidationAttribute
    {
        public string NumberStart { get; private set; }
        public string NumberEnd { get; private set; }

        public IntegerCompareAttribute(string numStart, string numEnd)
        {
            NumberStart = numStart;
            NumberEnd = numEnd;
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object numStart = properties.Find(NumberStart, true /* ignoreCase */).GetValue(value);
            object numEnd = properties.Find(NumberEnd, true /* ignoreCase */).GetValue(value);

            long lNumberStart = 0;
            long lNumberEnd = 1;
            try
            {
                // Get Value of the DateStart property
                lNumberStart = Convert.ToInt64(numStart);
                lNumberEnd = Convert.ToInt64(numEnd);
            }
            catch (Exception ex)
            {
            }

            return lNumberEnd > lNumberStart;
        }
    }
}
